<!-- 上传省平台 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="支付单号" prop="documentNumber">
        <el-input
          v-model="queryParams.documentNumber"
          placeholder="请输入支付单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物流公司" prop="carrier">
        <el-input
          v-model="queryParams.carrier"
          placeholder="请输入物流公司"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="司机姓名" prop="driverName">
        <el-input
          v-model="queryParams.driverName"
          placeholder="请输入司机姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-upload"
          size="mini"
          :disabled="multiple"
          @click="handleUpload"
        >上传</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="paymentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="支付单号" align="center" prop="documentNumber" />
      <el-table-column label="物流公司" align="center" prop="carrier" show-overflow-tooltip />
      <!-- <el-table-column label="统一社会信用代码或证件号码" align="center" prop="actualCarrierId" /> -->
      <!-- <el-table-column label="车牌号" align="center" prop="vehicleNumber" />
      <el-table-column label="车牌颜色" align="center" prop="vehiclePlateColorCode" /> -->
      <el-table-column label="司机姓名" align="center" prop="driverName" />
      <el-table-column label="订单数" align="center" prop="orderTotal" />
      <el-table-column label="支付金额(元)" align="center" prop="monetaryAmount" />
      <!-- <el-table-column label="申请人" align="center" prop="applyUserName" /> -->
      <el-table-column label="支付人" align="center" prop="payUserName" />
      <el-table-column label="支付日" align="center" prop="payDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center" prop="payType" :formatter="payTypeFormat" />
      <!-- <el-table-column label="收款方名称" align="center" prop="recipient" /> -->
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <!-- <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="getOrderButton(scope.row)"
          >查看订单</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 上传进度对话框 -->
    <el-dialog title="正在上传，请稍候..." :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false" @close="close">
      <el-progress style="margin-left: 160px;" v-if="percentage >= 100" type="circle" :percentage="100" status="success"></el-progress>
      <el-progress style="margin-left: 160px;" v-else type="circle" :percentage="percentage"></el-progress>
    </el-dialog>

    <!--相关订单对话框-->
    <el-dialog :title="title" :visible.sync="openOrder" width="1200px" append-to-body>
      <order-list ref="orderRef"></order-list>
    </el-dialog>
  </div>
</template>

<script>
import { listPayment, uploadPay } from "@/api/tws/payment";
import OrderList from '@/components/OrderList/index'

export default {
  name: "Uploads",
  components: {
    OrderList
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      percentage: 0,
      // 支付状态字典
      statusOptions: [],
      // 支付表格数据
      paymentList: [],
      // 是否显示弹出层
      open: false,
      //相关订单弹出层
      openOrder:false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        documentNumber: null,
        carrier: null,
        actualCarrierId: null,
        vehicleNumber: null,
        vehiclePlateColorCode: null,
        driverId: null,
        driverName: null,
        orderTotal: null,
        monetaryAmount: null,
        applyUserName: null,
        payUserId: null,
        payUserName: null,
        payDate: null,
        payType: null,
        paymentMeansCode: null,
        recipient: null,
        receiptAccount: null,
        bankCode: null,
        sequenceCode: null,
        status: null,
        isSendToPro: null,
        sendToProDateTime: null,
        tenantId: null,
      }
    };
  },
  created() {
    this.queryParams.status = this.$store.state.constants.payStatus_yzf;
    this.queryParams.isSendToPro = '0';
    this.getDict();
    this.getList();
  },
  methods: {
    getDict() {
      // 支付状态字典
      this.getDicts("payment_status").then(response => {
        this.statusOptions = response.data;
      });
    },
    /** 查询支付列表 */
    getList() {
      this.loading = true;
      listPayment(this.queryParams).then(response => {
        this.paymentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 支付方式翻译
    payTypeFormat(row, column) {
      return row.payType == '1' ? '线下支付' : '在线支付';
    },
    // 支付状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.status = this.$store.state.constants.payStatus_yzf;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查看订单按钮操作 */
    getOrderButton(row){
      const id = row.id
      const type = 2; //1为开票 2为支付
      this.title = "相关订单"
      this.openOrder = true;
      this.$nextTick(() =>{
        this.$refs.orderRef.getList(id,type);
      })

    },
    // 上传
    handleUpload(row) {
      const ids = row.id || this.ids;
      let that = this;
      this.$confirm('是否确认上传至省平台?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        that.open = true;
        that.up(ids, 0);
      }).catch(err => {
        that.getList();
      })
    },
    up(ids, ind) {
      let that = this;
      let len = ids.length;
      uploadPay(ids[ind]).then(res => {
        ind++;
        that.percentage = Number(((ind / len) * 100).toFixed(1));
        if(ind >= len) {
          setTimeout(() => {
            that.$modal.msgSuccess("上传成功");
            that.open = false;
          }, 1000)
        } else {
          that.up(ids, ind);
        }
      }).catch(err => {
        that.$modal.msgError(err.msg || "上传失败");
      })
    },
    close() {
      this.percentage = 0;
      this.getList();
    }
  }
};
</script>
